

clear

tic

Pgen = zeros (5);
cost = zeros (5);
mutated_Pgen = zeros (5);
m_cost = zeros (5);
Pgen2 = zeros (5);
cost2 = zeros(5);
mutated_Pgen2 = zeros(5);
m2_cost = zeros(5);


m = 24;     %hour = 24


%===========================================================================================================================
%                                   Input Data
%===========================================================================================================================


Demand = [ 700 750 800 820 850 900 950 970 1000 1050 1100 1200 1180 1150 1110 1050 1000 1100 1200 1150 1080 1100 900 800 ];

Pmax = [ 455 455 130 130 162 80	85 55 55 55 ];
Pmin = [ 150 150 20 20 25 20 25 10 10 10 ];

a = [ 1000 970 700 680 450 370 480 660 665 670 ];
b = [ 16.19 17.26 16.60 16.50 19.70 22.26 27.74 25.92 27.27 27.79 ];
c = [ 0.00048 0.00031 0.002 0.00211 0.00398 0.00712 0.00079 0.00413 0.00222 0.00173 ];


%================================================================================================
%                               Initial population
%================================================================================================

for kpop = 1:4
   for g = 1:24
    for r=1:0194543827
        for f = 1:10
            
            Pgen(f,g,kpop) = round(((Pmax(1,f)-Pmin(1,f))*rand) + Pmin(1,f));
            cost(f,g,kpop) = a(1,f) + b(1,f)*Pgen(f,g,kpop) + c(1,f)*(Pgen(f,g,kpop))^2;
            totalcost(kpop,g) = sum(cost(:,g,kpop));
            daycost(kpop) = sum(totalcost(kpop,:));
       
        end
        totalPgen(g,kpop) = sum (Pgen (:,g,kpop))
        if totalPgen(g,kpop) == Demand (1,g)
            r, break
        end
    end
   end
end


%=============================================================================================================
%                                     Mutation 
%=============================================================================================================


% declaration for tau &taudash
tau = 1/((2*m^(1/2))^(1/2));
taudash = 1/(2*m^(1/2));

for i=1:30
    
    Beta(i)=B;
    
end

Beta0 = B;

for kpop2 = 1:4
   for l = 1:24
       for r=1:0194543827
            for k = 1:10
        
                BetaN = B;
                mutationfactor         = exp((taudash*Beta0)+(tau*BetaN));
                alpha                  = Pgen(k,l,kpop2)* mutationfactor;
                mutated_Pgen(k,l,kpop2)= round(Pgen(k,l,kpop2) + alpha*BetaN);
                m_cost(k,l,kpop2)      = a(1,k) + b(1,k)*mutated_Pgen(k,l,kpop2) + c(1,k)*(mutated_Pgen(k,l,kpop2))^2;
                m_totalcost(kpop2,l) = sum(m_cost(:,l,kpop2));
                m_daycost(kpop2) = sum(m_totalcost(kpop2,:));
    
            end
           total_mutatedPgen(l,kpop2) = sum (mutated_Pgen (:,l,kpop2))
            if total_mutatedPgen(l,kpop2) == Demand (1,l)
                r, break
            end
       end
   end
end

%=====================================================================================================
%                                 Combination
%=====================================================================================================


for dd=1:8
    if dd<=4
        combPgen (:,:,dd)=Pgen(:,:,dd);
    else
        ee=dd-4;
        combPgen(:,:,dd)= mutated_Pgen(:,:,ee);
    end
end


%=============================================================================================
%                                 Sorting
%=============================================================================================


combined_daycost  = [daycost m_daycost];

Sorted_c_daycost = sort(combined_daycost');

best_daycost = Sorted_c_daycost (1:4,1);


%=============================================================================================
%                              Locate/select
%=============================================================================================



for cc=1:4
    
    locate = find (combined_daycost(:)== best_daycost(cc));
    locations (1,cc) = locate;
    
    bestPgen(:,:,cc) = combPgen(:,:,locate);
    
end

%=============================================================================================
%                              convergence
%=============================================================================================


for zz = 1:0194543827
    
    Pgen2 = bestPgen;
    
    for kpop4 = 1:4
        for v = 1:24
            for r=1:0194543827
                for w = 1:10
                    
                    cost2(w,v,kpop4) = a(1,w) + b(1,w)*Pgen2(w,v,kpop4) + c(1,w)*(Pgen2(w,v,kpop4))^2;
                    totalcost2(kpop4,v) = sum(cost2(:,v,kpop4));
                    daycost2(kpop4) = sum(totalcost2(kpop4,:));
                    
                end
                totalPgen2(v,kpop4) = sum (Pgen2(:,v,kpop4))
                if totalPgen2(v,kpop4) == Demand (1,v)
                    r, break
                end
            end
        end
    end
    
    
    for i=1:30
        
        Beta(i)=B;
        
    end
    
    Beta0 = B;
    
    for kpop3 = 1:4
        for h = 1:24
            for r=1:0194543827
                for s = 1:10
                    
                    BetaN = B;
                    mutationfactor         = exp((taudash*Beta0)+(tau*BetaN));
                    alpha                  = Pgen2(s,h,kpop2)* mutationfactor;
                    mutated_Pgen2(s,h,kpop3)= round(Pgen2(s,h,kpop3) + alpha*BetaN);
                    m2_cost(s,h,kpop3)      = a(1,s) + b(1,s)*mutated_Pgen2(s,h,kpop3) + c(1,s)*(mutated_Pgen2(s,h,kpop3))^2;
                    m2_totalcost(kpop3,h) = sum(m2_cost(:,h,kpop3));
                    m2_daycost(kpop3) = sum(m2_totalcost(kpop3,:));
                    
                end
                total_mutatedPgen2(h,kpop3) = sum (mutated_Pgen2(:,h,kpop3))
                if total_mutatedPgen2(h,kpop3) == Demand(1,h)
                    r, break
                end
            end
        end
    end
    
    
    for kk=1:8
        if kk<=4
            combPgen2 (:,:,kk)=Pgen2(:,:,kk);
        else
            jj=dd-4;
            combPgen2(:,:,kk)= mutated_Pgen2(:,:,jj);
        end
    end
    
    
    combined_daycost2  = [daycost2 m2_daycost];
    
    Sorted_c_daycost2 = sort(combined_daycost2');
    
    best_daycost2 = Sorted_c_daycost2 (1:4,1);
    
   
    % locations
    
    locate_1 = find (combined_daycost2(:)== best_daycost2(1));
    locate_2 = find (combined_daycost2(:)== best_daycost2(2));
    locate_3 = find (combined_daycost2(:)== best_daycost2(3));
    locate_4 = find (combined_daycost2(:)== best_daycost2(4));
    
    
    input = [ locate_1' locate_2' locate_3' locate_4' ];
    
    locations2 = input (1,1:4);
     
    
    for keep = 1:4
        
        like = locations2 (1,keep);
        bestPgen(:,:,keep) = combPgen2(:,:,like);
        
    end
       
    if best_daycost2(1,1) == best_daycost2(4,1)
        zz, break
    end
    
end

result_of_bestPgen = bestPgen
with_day_cost = best_daycost2


toc


